<?php 
  /**
    *
    * FILE:          cieListQuery.php
    * LOCATION:      /root
    * REFERENCES:    admin.php
    *                cieSearch.php
    *                cieEdit.php
    * FUNCTION:      displays a table of consumers that are
    *                returned from the database as a resultset
    *                based on search parameters from cieSearch.php
    * DEPENDENCIES:  db_connect.php 
    * ==============================================================================
    * 
    * @version $Id: admin.php,v 0.1 2008/08/08
    * @copyright Copyright (c) none
    * @author ChrisV
    * @license http://opensource.org/licenses/gpl-license.php GNU General 
    * Public License Version 2 (GPL v2)
    * ==============================================================================
    */
  //connect to database
	include('includes/db_connect.php');
	include_once('./classes/pagination.class.php');
	//get search parameters and sanitize
	$searchStr  = sanitize($_POST['searchStr']);
	$searchAttr = sanitize($_POST['searchAttr']);
	//echo "Search String: ".$searchStr." Search Attribute: ".$searchAttr."<br/>";
	//set query request to match correct database field
	if($searchAttr=='lName') {
   //query by last name
		$col = 'c.last_name';
	} else if($searchAttr=='fName') {
   //query by first name
		$col = 'c.first_name';
	} else if($searchAttr=='city') {
   //query by city
		$col = 'c.city';
	} else if($searchAttr=='phone') {
   //query by phone number
		$col = 'c.phone';
	}else if($searchAttr=='false') {
   //no query option chosen
		$col = null;
	}
	//set up pagination
	$items = 10;  //items per page
	$page = 1;
	$get_page = 0;
	if(isset($_GET['page'])){$get_page = sanitize($_GET['page']);}else{$get_page = NULL;} 
	//BEGIN PAGINATION CODE
  if(isset($get_page) and is_numeric($get_page) and $page != $get_page){
			$limit = " LIMIT ".(($page-1)*$items).",$items";
 }
	if($col != null) {
 $sqlStr = "SELECT c.id, 
														c.last_name, 
                    c.first_name, 
                    c.city, 
                    c.phone, 
                    c.address, 
                    c.zip, 
                    c.phone, 
                    ls.name as state
             FROM   consumer c, 
                    lst_state ls 
             WHERE  ".$col." 
             LIKE \"%".$searchStr."%\" 
             AND    c.state_id = ls.id 
             ORDER BY c.last_name asc";
	$query1 = mysql_query($sqlStr);
	$total_returned = mysql_num_rows($query1);
	$query = mysql_query($sqlStr.$limit);
   if($total_returned < 1){
			//WE RAN THE QUERY BUT NOTHING MATCHED
?>   
				<img name="error" src="./images/cancel.png" style="float:left;padding:5px;"/>
					<h2 style="font-weight:bold; font-size:1.2em;margin-left:55px;">
						<div class="gt-error">
							Sorry. There were no results that matched your
							request.
							<a href="./admin.php?action=cie"
								name="search_again"
								id="search_again"
								title="Try another search"
								style="color:#F3F781;">Would you like to search again?</a>							
							<br />
							The query you entered was:
							<br />
							<div class="gt-notice-box3">
								Search String Entered: <?php echo $searchStr;?><br />
								Search Attribute Chosen: <?echo $searchAttr;?><br  />
							</div>
						</div>
					</h2>
					<h2 style="padding-left:55px;">What Would You Like To Do Next?</h2>
					<p style="padding-left:55px;">
						<!-- notice that to create new consumer id, id is set to 0 --> 
						<a style="background-image:url('./images/gt/btn-blank-long.jpg');
											background-repeat:no-repeat;
											border:none;
											line-height:3em;
											margin-right:30px;
											padding:7px 25px 7px 25px;
											width:160px;
											height: 30px;
											color: #fff;
											font-weight:bold;
											text-decoration:none;" 
							class="btn"
							href="admin.php?action=cielistall">
							<strong>See All Case Files</strong>
						</a>
						<!-- option to create new record -->
						<!-- notice that to create new consumer id, id is set to 0 -->
						<a style="background-image:url('./images/gt/btn-blank-long.jpg');
											background-repeat:no-repeat;
											border:none;
											line-height:3em;
											padding:7px 25px 7px 15px;
											width:160px;
											height: 30px;
											color: #fff;
											font-weight:bold;
											text-decoration:none;" 
							class="btn" href="admin.php?action=cieedit&id=0">
							<strong>Create New Case File</strong>
						</a>
					</p>
<?php
      exit();
    }
  //OK. WE FOUND SOME MATCHES, SO HANDLE THEM
  echo "<h1 class=\"gt-notice-box\">
						<span style=\"font-size:2.5em; font-weight:bold; color:#00FF00;\">"
							.$total_returned.
						"</span>  Consumer case files matched your search request.</h1>";
   //style counter
	 $i  = 0;
	 if($total_returned >0){
			$p = new pagination;
			$p->Items($total_returned);
			$p->limit($items);
			$p->target("admin.php?action=cielistquery");
			$p->currentPage($page);
			$p->calculate();
			$p->changeClass("pagination");
?>
            <table class="gt-list-table" border="0">
                 <thead> 
                     <tr>
                         <th>Full Name</th>
                         <th>Address</th>
                         <th>City</th>
                         <th>State</th>
                         <th>Zip</th>
                         <th>Phone</th>
                     </tr>
                 </thead>
                 <tbody>
<?php
      while($row=mysql_fetch_array($query)) { 
					if($i%2==0) {	 
						echo "<tr class='even'>";
					}else {
						echo "<tr class='odd'>";
					}
?>
                     <td>
								<a href="admin.php?action=cieedit&id=<?php echo $row["id"]; ?>">
                                    <?php echo $row["last_name"].", ".$row["first_name"]; ?>
                                </a>
                     </td>
                     <td><?php echo $row["address"]; ?></td>
                     <td><?php echo $row["city"]; ?></td>
                     <td><?php echo $row["state"]; ?></td>
                     <td><?php echo $row["zip"]; ?></td>
                     <td><?php echo $row["phone"]; ?></td>
                 </tr>
<?php
      $i++; 
     }//end result set while loop 
?>
				</tbody>
             </table>
             <!-- END RESULTSET TABLE -->
<?php 
			//if we results is greater than the limit, show navigation
			/*
			if($total_returned > $items){
				echo"<p style=\"margin-top: 15px; font-size:1.2em; font-weight:bold;\"> Navigate Your Search Results: ";
							$p->show();
				echo"</p>";
			}
			*/
  }else{  
				//put error handling here for bad db connection
?>
				<img name="error" src="./images/cancel.png" style="float:left;padding:5px;"/>
					<h2 style="font-weight:bold; font-size:1.2em;margin-left:55px;">
						<div class="gt-error">
							We received your search request but had an internal problem. <br /> 
							You correctly entered the following information:
							<br />
							<div class="gt-notice-box3">
								Search String Entered: <?php echo $searchStr;?><br />
								Search Attribute Chosen: <?echo $searchAttr;?><br  />
							</div>
						</div>
					</h2>
					<h2 style="padding-left:55px;">What Would You Like To Do Next?</h2>
					<p style="padding-left:55px;">
						<!-- notice that to create new consumer id, id is set to 0 --> 
						<a style="background-image:url('./images/gt/btn-blank-long.jpg');
											background-repeat:no-repeat;
											border:none;
											line-height:3em;
											margin-right:30px;
											padding:7px 25px 7px 25px;
											width:160px;
											height: 30px;
											color: #fff;
											font-weight:bold;
											text-decoration:none;" 
							class="btn"
							href="admin.php?action=cielistall">
							<strong>See All Case Files</strong>
						</a>
						<!-- option to create new record -->
						<!-- notice that to create new consumer id, id is set to 0 -->
						<a style="background-image:url('./images/gt/btn-blank-long.jpg');
											background-repeat:no-repeat;
											border:none;
											line-height:3em;
											padding:7px 25px 7px 15px;
											width:160px;
											height: 30px;
											color: #fff;
											font-weight:bold;
											text-decoration:none;" 
							class="btn" href="admin.php?action=cieedit&id=0">
							<strong>Create New Case File</strong>
						</a>
					</p>
<?php 
      exit();
  }
}else{
		//no query option was chosen
?>
	<img name="error" src="./images/cancel.png" style="float:left;padding:5px;"/>
		<h2 style="font-weight:bold; font-size:1.2em;margin-left:55px;">
			<div class="gt-error">
						Sorry. No results were found.  You didn't pick a search parameter option.<br />
						<a href="./admin.php?action=cie"
								name="search_again"
								id="search_again"
								title="Try another search"
								style="color:#F3F781;">Would you like to search again?</a>
			</div>
		</h2>
	 <!-- user control buttons -->
	<p style="float:right;">
				<!-- notice that to create new consumer id, id is set to 0 --> 
				<a style="background-image:url('./images/gt/btn-blank-long.jpg');
											background-repeat:no-repeat;
											border:none;
											line-height:3em;
											margin-right:30px;
											padding:7px 25px 7px 25px;
											width:160px;
											height: 30px;
											color: #fff;
											font-weight:bold;
											text-decoration:none;" 
							class="btn"
						href="admin.php?action=cielistall">
						<strong>See All Case Files</strong>
			</a>
    <!-- option to create new record -->
       <!-- notice that to create new consumer id, id is set to 0 -->
				<a style="background-image:url('./images/gt/btn-blank-long.jpg');
											background-repeat:no-repeat;
											border:none;
											line-height:3em;
											padding:7px 25px 7px 15px;
											width:160px;
											height: 30px;
											color: #fff;
											font-weight:bold;
											text-decoration:none;" 
							class="btn" href="admin.php?action=cieedit&id=0">
				<strong>Create New Case File</strong>
		</a>
    </p>
<?php 
      exit();
   } 
?>
   <br/>
	 <!-- user control buttons -->
	<p style="float:right;">
				<!-- notice that to create new consumer id, id is set to 0 --> 
				<a style="background-image:url('./images/gt/btn-blank-long.jpg');
											background-repeat:no-repeat;
											border:none;
											line-height:3em;
											margin-right:30px;
											padding:7px 25px 7px 25px;
											width:160px;
											height: 30px;
											color: #fff;
											font-weight:bold;
											text-decoration:none;" 
							class="btn"
						href="admin.php?action=cielistall">
						<strong>See All Case Files</strong>
			</a>
    <!-- option to create new record -->
       <!-- notice that to create new consumer id, id is set to 0 -->
				<a style="background-image:url('./images/gt/btn-blank-long.jpg');
											background-repeat:no-repeat;
											border:none;
											line-height:3em;
											padding:7px 25px 7px 15px;
											width:160px;
											height: 30px;
											color: #fff;
											font-weight:bold;
											text-decoration:none;" 
							class="btn" href="admin.php?action=cieedit&id=0">
				<strong>Create New Case File</strong>
		</a>
    </p>